/**
 * Copyright (C), 2019-2022, 宇信融汇
 * FileName: Publisher
 * Author:   666
 * Date:     2022/4/23 16:33
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 666           修改时间           版本号              描述
 */
package com.mashibing.header;

import com.mashibing.util.RabbitMQConnectionUtil;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @Classname Publisher
 * @Description TODO
 * @Date 2022/4/23 16:33
 * @Created by 666
 */
public class Publisher {

    public static final String HEADER_EXCHANGE = "header_exchange";
    public static final String HEADER_QUEUE = "header_queue";

    @Test
    public void publish() throws Exception {
        //1、创建connection
        Connection connection = RabbitMQConnectionUtil.getConnection();

        //2、创建管道channels
        Channel channel = connection.createChannel();
        //3、构建交换机
        channel.exchangeDeclare(HEADER_EXCHANGE, BuiltinExchangeType.HEADERS);
        //4、构建队列

        channel.queueDeclare(HEADER_QUEUE, true, false, false, null);
        //5、绑定交换机和队列
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-match", "all");
        arguments.put("name", "jack");
        arguments.put("age", "22");
        channel.queueBind(HEADER_QUEUE, HEADER_EXCHANGE,"" , arguments);

        //6、发送消息
        String msg = "header测试消息！";
        Map<String, Object> headers = new HashMap<>();
        headers.put("name", "jack");
        headers.put("age", "22");
        AMQP.BasicProperties build = new AMQP.BasicProperties().builder().headers(headers).build();
        channel.basicPublish(HEADER_EXCHANGE, "", build, msg.getBytes());

    }
}
